package com.runfast.waimai.web.controller.Runfastbusinesscontroller;

import com.google.common.base.Joiner;
import com.runfast.common.utils.SessionContext;
import com.runfast.paotui.dao.model.RunfastAgentbusinessExample;
import com.runfast.paotui.service.RunfastAgentbusinessService;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.RunfastGoodsSellRecord;
import com.runfast.waimai.dao.model.RunfastWithdraw;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentaccounts;
import com.runfast.waimai.service.RunfastBusinessService;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.service.RunfastWithdrawService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/*商家销售统计 代理商销售统计 商家提现申请查询 商家提现记录查询
 * @author mengxiang
 * @create 2018-05-30 15:27
 * */
@RestController
//@RequestMapping("/business")
@RequestMapping("/server/business")
public class runfastbusinesscontroller {
    @Resource
    RunfastBusinessService businessService;
    @Resource
    RunfastGoodsSellRecordService runfastGoodsSellRecordService;
    @Resource
    RunfastWithdrawService service;//提现表
    @Resource
    RunfastAgentbusinessService runfastAgentbusinessService;
    //    @PostMapping("dirbusinessmoney") //商家销售统计 差数据 下面一个接口从定单列表中获取，要计算总和撒的
//    public @ResponseBody ModelMap dirbusinessmoney(Integer agentid,String name,String createtime1,String createtime2) throws ParseException {//代理商ID ;//商家名称 开始时间，结束时间
//        ModelMap model=new ModelMap();
//        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        Date date1=dateFormat.parse(createtime1);
//        Date date2=dateFormat.parse(createtime2);
//       if(agentid!=null&&createtime1!=null&&createtime2!=null){
//           List<RunfastBusiness> businessList=businessService.findByAgentnameAndbusinessnameAndTime(agentid,name,date1,date2);
//           if(businessList!=null){
//               model.put("list",businessList);
//           }else {model.put("msg","没有附合条件的结果"); }
//       }else {
//           model.put("msg","输入的的条件不能为空");
//       }
//        return  model;
//    }
    //  @PostMapping("/dirbusinessmoney1") //商家销售统计 差数据 从定单列表中获取，要计算总和撒的 第五张表 business/dirbusinessmoney1
    @GetMapping("/dirbusinessmoney1")
    public @ResponseBody ModelMap dirbusinessmoney1(String agentid,String name,String createtime1,String createtime2,Integer isdelive,Integer page,Integer pagesize) throws ParseException {//代理商ID ;//商家名称 开始时间，结束时间
        ModelMap model=new ModelMap();
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();

        if(admin!=null && admin.getType()==2 && agentAccounts!=null){ //代理商登陸
            agentid= String.valueOf(agentAccounts.getAgentid());
        }

        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        Date date1=dateFormat.parse(createtime1+" 00:00:00");
        Date date2=dateFormat.parse(createtime2+" 23:59:59");
        Integer page1=page*pagesize;
        if(agentid==null||agentid.equals("")){
            List<Map<String,String>> map =runfastGoodsSellRecordService.findByAgentnameAndbusinessnameAndTime(null,name,date1,date2 ,isdelive,page1,pagesize);
            Integer count =runfastGoodsSellRecordService.findByAgentnameAndbusinessnameAndTimecount(null,name,date1,date2 ,isdelive,page1,pagesize);
            if(map!=null){

                model.put("list",map);
                model.put("count",count);
                model.put("success",true);
            }else {model.put("msg","没有附合条件的结果");
                model.put("success",false);}
        }else {

            //接收的为list map 集合
            List<String> result = Arrays.asList(agentid.split(","));
            List<Integer>list=new ArrayList<>();
            for(String x:result){
                list.add(Integer.parseInt(x));
            }
            List<Map<String,String>> map =runfastGoodsSellRecordService.findByAgentnameAndbusinessnameAndTime(list,name,date1,date2 ,isdelive,page1,pagesize);
            Integer count =runfastGoodsSellRecordService.findByAgentnameAndbusinessnameAndTimecount(list,name,date1,date2 ,isdelive,page1,pagesize);
            if(map!=null){

                model.put("list",map);
                model.put("count",count);
                model.put("success",true);
            }else {model.put("msg","没有附合条件的结果");
                model.put("success",false);}
        }
        return  model;
    }
    /*财务统计
     * */
//@PostMapping("/agentnamemoney") //第一张表
    @GetMapping("/agentnamemoney")

    public @ResponseBody ModelMap agentnamemoney(String agentId,String createtime1,  String createtime2, Integer isdelive,@RequestParam(value = "page",required = false,defaultValue = "0") Integer page,@RequestParam(value = "pages",required = false,defaultValue = "10")Integer pages)  {
        //String createtime1,  String createtime2, Integer isdelive
//    System.out.println(createtime1);

        ModelMap model=new ModelMap();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");

        Integer page1=page*pages;
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();

        if(admin!=null && admin.getType()==2 && agentAccounts!=null){ //代理商登陸
            agentId= String.valueOf( agentAccounts.getAgentid());
        }

        try {
            if(createtime1 !=null&&createtime2 !=null){
                Date date1=format.parse(createtime1+" 00:00:00");
                Date date2=format.parse(createtime2+" 23:59:59" );
                if(agentId==null||agentId.equals("")){
                List<Map<String,String>> list= runfastGoodsSellRecordService.findAgentnameMoney(null,date1,date2,isdelive,page1,pages);
                Integer count = runfastGoodsSellRecordService.findAgentnameMoneycount(null,date1,date2,isdelive,page1,pages);
                if(list!=null){
                    if(list.size()>0){
                        model.put("list",list);
                        model.put("count",list.size());
                        model.put("success",true);
                    }else {
                        RunfastAgentbusinessExample runfastAgentbusinessExample=new RunfastAgentbusinessExample();
                        Integer a=  runfastAgentbusinessService.countByExample(runfastAgentbusinessExample);

                        model.put("list",list);
                        model.put("count",a);
                        model.put("success",true);
                    }

                }else {model.put("msg","查无结果");
                    model.put("success",false);}
                }else {
                    List<String> result = Arrays.asList(agentId.split(","));
                    List<Integer> list1 = new ArrayList<>();
                    for (String x : result) {
                        list1.add(Integer.parseInt(x));
                    }
                    List<Map<String, String>> list = runfastGoodsSellRecordService.findAgentnameMoney(list1, date1, date2, isdelive, page1, pages);
                    Integer count = runfastGoodsSellRecordService.findAgentnameMoneycount(list1, date1, date2, isdelive, page1, pages);
                    if (list != null) {
                        if (list.size()>0) {
                            model.put("list", list);
                            model.put("count", list.size());
                            model.put("success", true);
                        } else {
                            RunfastAgentbusinessExample runfastAgentbusinessExample = new RunfastAgentbusinessExample();
                            Integer a = runfastAgentbusinessService.countByExample(runfastAgentbusinessExample);

                            model.put("list", list);
                            model.put("count", a);
                            model.put("success", true);
                        }

                    }
            }

            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return  model;

    }
    //商家提现申请查询 第4张表
    //  @PostMapping("/moneyhome")
    @GetMapping("/moneyhome")
    public @ResponseBody ModelMap moneyhome(String agentId,String name, Integer state,Integer status,String usermobile,String createtime1,String createtime2,@RequestParam(value = "page",required = false,defaultValue = "0") Integer page,@RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize) throws ParseException {
        /*String name;//商家名称  Integer state;//是否已处理1是0否 Integer status;// 状态0待审核,1通过，2不通过  String usermobile;//客户手机号
         * */
        System.out.println(state+"state"+status+"status**************************************************************");
        ModelMap model=new ModelMap();

        Map<Object,Object> map1=new HashMap<Object, Object>();
        List<String>agentids=new ArrayList<>();
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();

        if(admin!=null && admin.getType()==2 && agentAccounts!=null){ //代理商登陸
            agentId= String.valueOf( agentAccounts.getAgentid());
        }
        Integer page1=page*pagesize;
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        Date date1=format.parse(createtime1+" 00:00:00");
        Date date2=format.parse(createtime2+" 23:59:59");

            if(agentId==null||agentId==""){
                List<RunfastWithdraw> runfastWithdraws=service.findMoneyhome(null,name,state,status,usermobile,date1,date2,page1,pagesize);
                Integer count= service.findMoneyhomecount(null,name,state,status,usermobile,date1,date2,page1,pagesize);
                if(runfastWithdraws!=null){
                    model.put("list",runfastWithdraws);
                    model.put("count",count);
                    model.put("success",true);
                }else {model.put("msg","你要的数据不存在");
                    model.put("success",false);}
            }else {
                List<String> result = Arrays.asList(agentId.split(","));
                List<Integer> list1 = new ArrayList<>();
                for (String x : result) {
                    list1.add(Integer.parseInt(x));
                }
                List<RunfastWithdraw> runfastWithdraws=service.findMoneyhome(list1,name,state,status,usermobile,date1,date2,page1,pagesize);
                Integer count= service.findMoneyhomecount(list1,name,state,status,usermobile,date1,date2,page1,pagesize);
                if(runfastWithdraws!=null){
                    model.put("list",runfastWithdraws);
                    model.put("count",count);
                    model.put("success",true);
                }else {model.put("msg","你要的数据不存在");
                    model.put("success",false);}
            }






        return model;
    }
    //商家提现记录查询 第三张表
    // @PostMapping("/businessmoneyhome")
    @GetMapping("/businessmoneyhome")
    public @ResponseBody ModelMap businessmoneyhome(String name,String agentId, String createtime1,String createtime2,@RequestParam(value = "page",required = false,defaultValue = "0")Integer page,@RequestParam(value = "pagesize",required = false,defaultValue = "10")Integer pagesize) throws ParseException {
        /*String agentname;//代理商名称  Integer state;//是否已处理1是0否 Integer status;// 状态0待审核,1通过，2不通过  String usermobile;//客户手机号
         * */
        ModelMap model=new ModelMap();
        Map<Object,Object> map1=new HashMap<Object, Object>();
        List<String>agentids=new ArrayList<>();
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();

        if(admin!=null && admin.getType()==2 && agentAccounts!=null){ //代理商登陸
            agentId= String.valueOf( agentAccounts.getAgentid());
        }
        Integer page1=page*pagesize;
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        Date date1=format.parse(createtime1+" 00:00:00");
        Date date2=format.parse(createtime2+" 23:59:59");
        if(name==null){
            model.put("msg","其它为空就原谅你了，但商家名不能为空");
            model.put("success",false);
        }else {
            if (agentId == null || agentId.equals("")) {
                List<RunfastWithdraw> runfastWithdraws = service.findBusinessMoneyhome(name, null, date1, date2, page1, pagesize);
                Integer count = service.findBusinessMoneyhomecount(name, null, date1, date2, page1, pagesize);
                if (runfastWithdraws != null) {
                    model.put("list", runfastWithdraws);
                    model.put("count", count);
                    model.put("success", true);
                } else {
                    model.put("msg", "你要的数据不存在");
                    model.put("success", false);
                }
            } else {
                List<String> result = Arrays.asList(agentId.split(","));
                List<Integer> list1 = new ArrayList<>();
                for (String x : result) {
                    list1.add(Integer.parseInt(x));
                }
                List<RunfastWithdraw> runfastWithdraws = service.findBusinessMoneyhome(name, list1, date1, date2, page1, pagesize);
                Integer count = service.findBusinessMoneyhomecount(name, list1, date1, date2, page1, pagesize);
                if (runfastWithdraws != null) {
                    model.put("list", runfastWithdraws);
                    model.put("count", count);
                    model.put("success", true);
                } else {
                    model.put("msg", "你要的数据不存在");
                    model.put("success", false);
                }
            }
        }
        return model;
    }
    /*商家提现申请修改
     * */
    // @PostMapping("/updatestatus")
    @GetMapping("/updatestatus")
    public @ResponseBody ModelMap updatestatus(Integer state,Integer status,String usermobile,Integer id){
        //       System.out.println("111111111111111111111111111111111111111111111111111111111111");
        //id 为申请提现序列id
        ModelMap model=new ModelMap();
        //  DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//
//            Date date=format.parse(createtime);
//            System.out.println(usermobile+createtime+11111);
//            RunfastWithdraw runfastWithdraw=service.findMobileAndTime(usermobile,date);
        //      System.out.println(status+"******************************");
        // state是否已处理1是0否
        // status;// 状态0待审核,1通过，2不通过
        RunfastWithdraw runfastWithdraw=service.findByid(id);

        if(runfastWithdraw !=null){
            if(state==0){

                if(status==0){
                    List<RunfastGoodsSellRecord> list=runfastGoodsSellRecordService.findBybussinessWithdraw(runfastWithdraw.getUserid());
                    for(RunfastGoodsSellRecord s:list){

                        s.setIswithdraw(0);//设为末处理
                        runfastGoodsSellRecordService.updateByPrimaryKey(s);
                    }
                    model.put("msg","待审核");
                    model.put("success",false);
                }
                if(status==1){
                    List<RunfastGoodsSellRecord> list=runfastGoodsSellRecordService.findBybussinessWithdraw(runfastWithdraw.getUserid());
                    for(RunfastGoodsSellRecord s:list){

                        s.setIswithdraw(1);//设为已处理
                        runfastGoodsSellRecordService.updateByPrimaryKey(s);
                    }
                    model.put("msg","已通过");
                    model.put("success",true);
                }
                if(status==2){
                    model.put("msg","不通过");
                    model.put("success",false);
                }

            }else {

                model.put("msg","已处理");
                model.put("success",true);
            }

            runfastWithdraw.setState(state);
            runfastWithdraw.setStatus(status);
            service.updateByPrimaryKeySelective(runfastWithdraw);
            RunfastWithdraw runfastWithdraw1=service.findByid(id);
            model.put("runfastWithdraw1",runfastWithdraw1);
            model.put("success",true);
        }else {model.put("msg","你要的数据不存在");
            model.put("success",false);}


        return model;
    }

}
